package com.weiun.common.core.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.weiun.common.base.bean.PageResult;
import com.weiun.common.base.bean.ParamBean;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * The type Base service.
 *
 * @param <M>      the type parameter
 * @param <Record> the type parameter
 */
public class BaseService<M extends Mapper, Record> {

    protected final String PARAM_KEY_PAGENUM = "pageNum";
    protected final String PARAM_KEY_PAGESIZE = "pageSize";

    /**
     * The Mapper.
     */
    @Autowired
    protected M mapper;

    /**
     * Insert selective int.
     *
     * @param record the record
     * @return the int
     */
    public int insertSelective(Record record) {
        return mapper.insertSelective(record);
    }

    /**
     * Delete by primary key int.
     *
     * @param pk the pk
     * @return the int
     */
    public int deleteByPrimaryKey(Object pk) {
        return mapper.deleteByPrimaryKey(pk);
    }

    /**
     * Delete by example int.
     *
     * @param example the example
     * @return the int
     */
    public int deleteByExample(Example example) {
        return mapper.deleteByExample(example);
    }

    /**
     * Update by primary key selective int.
     *
     * @param record the record
     * @return the int
     */
    public int updateByPrimaryKeySelective(Record record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    /**
     * Update by example selective int.
     *
     * @param example the example
     * @param record  the record
     * @return the int
     */
    public int updateByExampleSelective(Example example, Record record) {
        return mapper.updateByExampleSelective(example, record);
    }

    /**
     * Select one by example record.
     *
     * @param example the example
     * @return the record
     */
    public Record selectOneByExample(Example example) {
        return (Record) mapper.selectOneByExample(example);
    }

    /**
     * Select all list.
     *
     * @return the list
     */
    public List<Record> selectAll() {
        return mapper.selectAll();
    }

    /**
     * Select count by example int.
     *
     * @param example the example
     * @return the int
     */
    public int selectCountByExample(Example example) {
        return mapper.selectCountByExample(example);
    }

    /**
     * Select by example list.
     *
     * @param example the example
     * @return the list
     */
    public List<Record> selectByExample(Example example) {
        return mapper.selectByExample(example);
    }

    /**
     * Page select by example page.
     * 分页查询方法
     *
     * @param page     the page
     * @param pageSize the page size
     * @param example  the example
     * @return the page
     */
    public PageResult<Record> pageSelectByExample(int page, int pageSize, Example example) {
        PageHelper.startPage(page, pageSize);
        Page<Record> list = (Page<Record>) mapper.selectByExample(example);
        return new PageResult<>(list);
    }

    /**
     * Gets page num.
     *
     * @param paramBean the param bean
     * @return the page num
     */
    public Integer getPageNum(ParamBean paramBean) {
        Integer pageNum = paramBean.getParam().getInteger(PARAM_KEY_PAGENUM);
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }
        return pageNum;
    }

    /**
     * Gets page size.
     *
     * @param paramBean the param bean
     * @return the page size
     */
    public Integer getPageSize(ParamBean paramBean) {
        Integer pageSize = paramBean.getParam().getInteger(PARAM_KEY_PAGESIZE);
        if (pageSize == null || pageSize.intValue() < 0) {
            pageSize = 20;
        }
        return pageSize;
    }

}
